package com.xiaoyu.lingdian.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.entity.CoreShoppingCart;
import com.xiaoyu.lingdian.service.CoreShoppingCartService;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import com.xiaoyu.lingdian.vo.CoreShoppingCartVO;

/**
 *购物车
 */
@Controller
@RequestMapping(value="/coreShoppingCart")
public class CoreShoppingCartController extends BaseController {
	
	@Autowired
	private CoreShoppingCartService coreShoppingCartService;	

	/**
	 * 查询用户购物车
	 * @param userId
	 * @param response
	 */
	@RequestMapping(value="/userCartView", method=RequestMethod.POST)
	public void userCartView(String userId, HttpServletResponse response) {
		logger.info("[CoreShoppingCartController.userCartView]userId:"+userId);
		List<CoreShoppingCartVO> vos = new ArrayList<CoreShoppingCartVO>();
		List<CoreShoppingCart> coreShoppingCarts = coreShoppingCartService.getCoreShoppingCartByUser(userId);
		CoreShoppingCartVO vo;
		for(CoreShoppingCart coreShoppingCart : coreShoppingCarts){
			vo = new CoreShoppingCartVO();
			vo.setCriglGoodsName(coreShoppingCart.getCriglGoodsName());
			vo.setCriglGoodsPrice(coreShoppingCart.getCriglGoodsPrice());
			vo.setCriglGoodsAmount(coreShoppingCart.getCriglGoodsAmount());
			vo.setCriglGoodsImg(coreShoppingCart.getCriglGoodsImg());
			vo.setCriglGoodsScore(coreShoppingCart.getCriglGoodsScore());
			vo.setCriglUser(coreShoppingCart.getCriglUser());
			
			vos.add(vo);
		}
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查询用户购物车信息成功！",vos), response);
		logger.info("[CoreShoppingCartController.userCartView]:end userCartView");
	}
	
	/**
	 * 将商品添加至购物车
	 * @param userId
	 * @param response
	 */
	@RequestMapping(value="/userCartAdd", method=RequestMethod.POST)
	public void userCartAdd(String userId, String goodsName, String goodsPrice, Integer goodsAmount, String goodsImg, Integer khdScore, String crpopUuid, HttpServletResponse response) {
		CoreShoppingCart coreShoppingCart = new CoreShoppingCart();
		coreShoppingCart.setCriglUser(userId);
		coreShoppingCart.setCriglGoodsName(goodsName);
		coreShoppingCart.setCriglGoodsPrice(goodsPrice);
		coreShoppingCart.setCriglGoodsAmount(goodsAmount);
		coreShoppingCart.setCriglGoodsImg(goodsImg);
		coreShoppingCart.setCriglGoodsScore(khdScore);
		String uuid = RandomUtil.generateString(16);
		coreShoppingCart.setCriglUuid(uuid);
		coreShoppingCart.setCriglCdate(new Date());
		coreShoppingCartService.insertCoreShoppingCart(coreShoppingCart);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "添加商品到购物车成功！"), response);
		logger.info("[CoreShoppingCartController.userCartAdd]:end userCartAdd");
	}
	
	/**
	 * 将更新购物车中商品信息
	 * @param userId
	 * @param response
	 */
	@RequestMapping(value="/userCartUpdate", method=RequestMethod.POST)
	public void userCartUpdate(String userId, String goodsName, Integer goodsAmount, HttpServletResponse response) {
		CoreShoppingCart coreShoppingCart = new CoreShoppingCart();
		coreShoppingCart.setCriglUser(userId);
		coreShoppingCart.setCriglGoodsName(goodsName);
		coreShoppingCart.setCriglGoodsAmount(goodsAmount);
		coreShoppingCart.setCriglUdate(new Date());
		coreShoppingCartService.updateCoreShoppingCart(coreShoppingCart);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "更新购物车商品成功！"), response);
		logger.info("[CoreShoppingCartController.userCartUpdate]:end userCartUpdate");
	}
	
	/**
	 * 从购物车中删除商品信息
	 * @param userId
	 * @param response
	 */
	@RequestMapping(value="/userCartDel", method=RequestMethod.POST)
	public void userCartDel(String uuid, HttpServletResponse response) {
		CoreShoppingCart coreShoppingCart = new CoreShoppingCart();
		coreShoppingCart.setCriglUuid(uuid);
		coreShoppingCartService.deleteCoreShoppingCart(coreShoppingCart);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "更新购物车商品成功！"), response);
		logger.info("[CoreShoppingCartController.userCartDel]:end userCartDel");
	}	
	
}